﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Net.Mime;
using Kooboo.CMS.Sites;
using Kooboo.CMS.Content.Models;
using Kooboo.CMS.DataModule.Services;
using Kooboo.CMS.DataModule.Models;

namespace Kooboo.CMS.DataModule.Areas.DataModule.Controllers
{
    public class ExportController : AreaControllerBase
    {
        private ExportService _exportService;

        public ExportController() :
            this(new ExportService())
        { }

        public ExportController(ExportService exportService)
        {
            _exportService = exportService;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(ExportModel model)
        {
            if(ModelState.IsValid)
            {
                Response.Clear();

                var data = _exportService.ExportAsZip(Repository.Current, model.FolderName);
                Response.ContentType = "application/x-zip-compressed";
                string headerValue = new ContentDisposition() { FileName = (model.FolderName + ".zip") }.ToString();
                Response.AddHeader("Content-Disposition", headerValue);

                Response.OutputStream.Write(data, 0, data.Length);
                Response.End();

                return null;
            }
            return View(model);
        }
    }
}